package com.chuanyun.wechatcheckin.controller;

import com.chuanyun.wechatcheckin.service.UserSyncService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户同步控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/user-sync")
@RequiredArgsConstructor
public class UserSyncController {
    
    private final UserSyncService userSyncService;
    
    /**
     * 同步企业微信用户ID到氚云
     */
    @PostMapping("/sync")
    public ResponseEntity<UserSyncService.SyncResult> syncUsers() {
        log.info("收到用户同步请求");
        
        try {
            UserSyncService.SyncResult result = userSyncService.syncUsersToH3yun();
            
            if (result.isSuccess()) {
                log.info("用户同步成功");
                return ResponseEntity.ok(result);
            } else {
                log.error("用户同步失败: {}", result.getMessage());
                return ResponseEntity.status(500).body(result);
            }
            
        } catch (Exception e) {
            log.error("用户同步异常", e);
            
            UserSyncService.SyncResult errorResult = new UserSyncService.SyncResult();
            errorResult.setSuccess(false);
            errorResult.setMessage("系统异常: " + e.getMessage());
            errorResult.setErrorDetail(e.toString());
            
            return ResponseEntity.status(500).body(errorResult);
        }
    }
}